package com.jiuyin.dianjing.util;

import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.util.Log;

import java.io.IOException;

/**
 * Mp3播放订单信息
 */
public class Mp3PlayerUtil {
    private static MediaPlayer mediaPlayer;

    public static void start(Context context, String type) {
        stop();
        String fileName = "order_wechat.mp3";
        try {
            AssetFileDescriptor fd = context.getAssets().openFd(fileName);
            if (null == mediaPlayer) {
                mediaPlayer = new MediaPlayer();
            }
            if (null != mediaPlayer) {
                mediaPlayer.setDataSource(fd.getFileDescriptor(), fd.getStartOffset(), fd.getLength());
                mediaPlayer.prepare();
                mediaPlayer.start();
            }
        } catch (IOException e) {
            LogUtil.log("Mp3PlayerUtil start " + Log.getStackTraceString(e));
        }
    }

    private static void stop() {
        if (mediaPlayer != null) {
            mediaPlayer.stop();
            mediaPlayer.reset();
        }
    }
}
